package br.ifrn.diacin.ctrlAula.apresentacao.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.ifrn.diacin.ctrlAula.apresentacao.funcoes.ActionsDelegate;
import br.ifrn.diacin.ctrlAula.integracao.model.Ambiente;
import br.ifrn.diacin.ctrlAula.integracao.model.TipoUsuario;
import br.ifrn.diacin.ctrlAula.integracao.model.Usuario;

/**
 * Servlet de controle que deve ser estendido pelos demais servlets
 */
public abstract class GenericController extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected HttpServletRequest request;
	protected HttpServletResponse response;
	private List<String> erros = new ArrayList<String>();
	private List<String> warns = new ArrayList<String>();
	private List<String> infos = new ArrayList<String>();
	protected final String PAGINA_DE_LOGIN = "/login.jsp";
	protected final String PAGINA_INDEX = "/index.jsp";

	public GenericController() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.request = request;
		this.response = response;
		if(request.getSession().isNew()){
			dispatch(PAGINA_DE_LOGIN);
		}
		String pagina = doPost();
		dispatch(pagina);
	}

	protected abstract String doPost() throws ServletException, IOException;

	protected void addErro(String erro) {
		erros.add(erro);
	}

	protected void addWarn(String warn) {
		warns.add(warn);
	}

	protected void addInfo(String info) {
		infos.add(info);
	}

	protected void dispatch(String pagina) throws ServletException, IOException {
		try {
			if (erros.size() != 0) {
				request.setAttribute("err", getMessages(erros));
				erros.clear();
			}
			if (warns.size() != 0) {
				request.setAttribute("warn", getMessages(warns));
				warns.clear();
			}
			if (infos.size() != 0) {
				request.setAttribute("info", getMessages(infos));
				infos.clear();
			}
		} catch (Exception e) {
			System.out.println("Deu erro aqui : Controller");
			e.printStackTrace();
		}
		try {
			RequestDispatcher rd = request.getRequestDispatcher(pagina);
			rd.forward(request, response);
		} catch (Exception e) {
			System.out.println("Deu erro aqui : Forward");
			e.printStackTrace();
		}
	}

	private String getMessages(List<String> lista) {
		String retorno = "";
		for (String s : lista) {
			retorno += s;
		}
		return retorno;
	}

	protected void render(String name, Object object) {
		this.request.setAttribute(name, object);
	}

	protected void renderSession(String name, Object object) {
		HttpSession session = this.request.getSession(true);
		session.setAttribute(name, object);
	}
	
	protected Object getSessionParameter(String nomeObjeto){
		return this.request.getSession().getAttribute(nomeObjeto);
	}
	
	protected String getParameter(String nomeParametro){
		return this.request.getParameter(nomeParametro);
	}
	
	protected String getRetornoTipoUsuario(){
		Usuario u = (Usuario) this.request.getSession().getAttribute("usuario");
		if(u.getTipoUsuario() == (TipoUsuario.COORDENADOR)){
			return "/coordenador.jsp";
		}else if(u.getTipoUsuario() == (TipoUsuario.PROFESSOR)){
			return "/professor.jsp";
		}else if(u.getTipoUsuario() == (TipoUsuario.BOLSISTA)){
			return "/bolsista.jsp";
		}return "index.jsp";
	}

}
